package com.stqf.scala.grammar

import com.alibaba.fastjson.JSON

import scala.io.Source

/**
  * 使用高阶函数多ClassPath先count_status_2018-01-31.log文件进行分析统计
  * Created by ASUS on 2018-02-01.
  */
object HigherOrderFunExercise {
  private val infoKey: String = "detail"
  private val numberKey: String = "任务生成量"
  private val filePath: String = "custom.log"

  def main(args: Array[String]): Unit = {
    val source = Source.fromFile(ClassLoader.getSystemResource(filePath).toURI)
    val lines: List[String] = source.getLines().toList
    var maps: Map[String, Int] = Map()
    lines.flatMap(line => {
      var totalLines: Seq[(String, Int)] = List[(String, Int)]()
      val info = JSON.parseObject(line).getJSONObject(infoKey)
      //val sets = scala.collection.convert.decorateAsScala.asScalaSetConverter(info.keySet())
      //println("info:" + sets.getClass.getName)
      info.keySet().forEach(key => {
        val num = info.getJSONObject(key).getIntValue(numberKey)
        totalLines = totalLines :+ (key, num)
      })
      totalLines
    }).foreach(temp => {
      maps.get(temp._1) match {
        case Some(value) => maps = maps.+((temp._1, value + temp._2))
        case None => maps = maps.+((temp._1, temp._2))
      }
    })
    source.close()
    maps.toList.sortBy(_._2).reverse.foreach(temp => println(temp._1 + ":" + temp._2))
  }

}
